//
//  TagsCell.swift
//  Employee
//
//  Created by swordjoy on 2017/5/11.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

class TagsCell: UITableViewCell {
    static var identifier = "TagsCell.swordjoy"
  
    static func height(with titles: [String]) -> CGFloat {
        if titles.isEmpty { return 40 }

        var currentX: CGFloat = 0
        var currentY: CGFloat = 0
        var rowCount = 0
        var columnCount = 0

        let labelHeight = UIFont.calculateHeight(width: 200, font: GlobalConfig.system12Font, text: "世外乡村")

        for title in titles {
            var width = UIFont.calculateWidth(font: GlobalConfig.system12Font, text: title)
            if width > UIScreen.width - 40 { width = UIScreen.width - 40 }

            // 如果宽度大于视图宽度,占一行
            if currentX + width + (SJTagsView.PYSEARCH_MARGIN * rowCount).cgFloat > UIScreen.width - 40 {
                columnCount += 1
                currentY += labelHeight
                rowCount = 1
                currentX = width
            }
            else {
                currentX += width
                rowCount += 1
            }
        }
        return (columnCount + 1).cgFloat * (24 + labelHeight) + 30
    }
  
    var titles: [String] = [] {
        didSet { tagsView.titles = titles }
    }
  
    fileprivate var tagsView: SJTagsView!
  
    // MARK: - life cycle
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = UIColor.white
        selectionStyle = .none

        create()
    }
  
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

// MARK: - fileprivate

extension TagsCell {
  
  fileprivate func create() {
    
    tagsView = SJTagsView()
    tagsView.backgroundColor = UIColor.clear
    addSubview(tagsView)
    tagsView.snp.makeConstraints { (make) in
      make.left.equalTo(20)
      make.right.equalTo(-20)
      make.top.bottom.equalToSuperview()
    }
    
  }
  
}
